<?php
/**手机版开奖公告**/
namespace app\mobile\controller;

use app\common\controller\Base;
use think\Db;
use app\common\logic\Trend;
use think\cache\driver\Redis;

class Lottery extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        $start = array(
            'user_all' => $this->user_all,
            'app_down' => $this->app_down,
            'logo' => $this->logo
        );
        $kjjg_id = input('get.kjjg_id');
        //判断彩种是否开启
        if(($kjjg_id && $this->lottery_if_open($kjjg_id) != 1)) {
            echo '<script>alert("抱歉，您无权访问该页面！");history.go(-1);</script>';exit;
        } 
        if($kjjg_id) {
            $kj_title = Db::name('caipai_list')->where('id=' . $kjjg_id)->field('cp_name_old,cp_name_new')->find();
            $this->kj_title = $kj_title['cp_name_new'] == '' ? $kj_title['cp_name_old'] : $kj_title['cp_name_new'];
        }
        $this->assign($start);
        $this->assign('cur','lottery');
    }

    /**
     * * 手机版开奖公告首页
     * * @param 【void】
     * * return
     */
    public function index()
    {
        $this->assign('title', '开奖大厅');
        $cp_id = [];
        $max_id_query = Db::name('kjjg_list')
				        ->field('MAX(id)')
				        ->where('kjjg','>','0')
				        ->group('czid')
				        ->buildSql();
        
        $max_czid_query = Db::name('kjjg_list')
					        ->field('czid,kjjg,qihao,jztime')
					        ->where("id IN({$max_id_query})")
					        ->buildSql();
        //echo $max_czid_query;exit();
        $caipai_list = Db::name('caipai_list')
        				->alias('a')
        				->join("{$max_czid_query} b",'b.czid=a.id')
        				->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
        				->where('a.cp_if_open<2')
			            ->order('a.cp_mb_px desc')
			            ->select();
        foreach ($caipai_list as &$caipai_single) {
        	$cp_id[] = $caipai_single['id'];
            $caipai_single['cp_name_new'] || $caipai_single['cp_name_new'] = $caipai_single['cp_name_old'];
            $caipai_single['kjjg'] = kjjg_split($caipai_single['kjjg'], $caipai_single['id']);
            $a=0;
            foreach ($caipai_single['kjjg'] as $ss){$a+=$ss;}
        }
        $this->assign('cp_id',json_encode($cp_id));
        $this->assign('caipai_list', $caipai_list);
       
        return $this->fetch('kjjg');
    }
    public function kjjg(){
    	$cp_id = [];
    	$max_id_query = Db::name('kjjg_list')
    	->field('MAX(id)')
    	->where('kjjg','>','0')
    	->group('czid')
    	->buildSql();
    	
    	$max_czid_query = Db::name('kjjg_list')
    	->field('czid,kjjg,qihao,jztime')
    	->where("id IN({$max_id_query})")
    	->buildSql();
    	//echo $max_czid_query;exit();
    	$caipai_list = Db::name('caipai_list')
    	->alias('a')
    	->join("{$max_czid_query} b",'b.czid=a.id')
    	->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
    	->where('a.cp_if_open<2')
    	->order('a.cp_mb_px desc')
    	->select();
    	foreach ($caipai_list as &$caipai_single) {
    		$caipai_single['jztime'] = date('Y-m-d H:i:s',$caipai_single['jztime']);
    		$cp_id[] = $caipai_single['id'];
    		$caipai_single['cp_name_new'] || $caipai_single['cp_name_new'] = $caipai_single['cp_name_old'];
    		$caipai_single['kjjg'] = kjjg_split($caipai_single['kjjg'], $caipai_single['id']);
    		$a=0;
    		foreach ($caipai_single['kjjg'] as $ss){$a+=$ss;}
    	}
    	return json($caipai_list);
    }
    public function indexAndroid()
    {
    	$this->assign('title', '开奖公告');
    	$max_id_query = Db::name('kjjg_list')
    	->field('MAX(id)')
    	->where('kjjg','>','0')
    	->group('czid')
    	->buildSql();
    
    	$max_czid_query = Db::name('kjjg_list')
    	->field('czid,kjjg,qihao,jztime')
    	->where("id IN({$max_id_query})")
    	->buildSql();
    	//echo $max_czid_query;exit();
    	$caipai_list = Db::name('caipai_list')
    	->alias('a')
    	->join("{$max_czid_query} b",'b.czid=a.id')
    	->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
    	->where('a.cp_if_open<2')
    	->order('a.cp_mb_px desc')
    	->select();
    	foreach ($caipai_list as &$caipai_single) {
    		$caipai_single['cp_name_new'] || $caipai_single['cp_name_new'] = $caipai_single['cp_name_old'];
    		$caipai_single['kjjg'] = kjjg_split($caipai_single['kjjg'], $caipai_single['id']);
    		$a=0;
    		foreach ($caipai_single['kjjg'] as $ss){$a+=$ss;}
    		if($caipai_single['id']==36 || $caipai_single['id']==44){
    			$caipai_single['kjjg'][]='=';
    			$caipai_single['kjjg'][]=$a;
    
    		}
    
    	}
    	//print_r($caipai_list);
    	$this->assign('caipai_list', $caipai_list);
    	return $this->fetch('kjjg2');
    }
    
    /**
     * 开奖公告详情
     * @return \think\mixed
     */
    public function runLottery()
    {
        $kjjg_id = input('get.kjjg_id');
        $max_id_query = Db::name('kjjg_list')
        ->field('MAX(id)')
        ->group('czid')
        ->buildSql();
        
        $max_czid_query = Db::name('kjjg_list')
        ->field('czid,kjjg,qihao,jztime')
        ->where("id IN({$max_id_query})")
        ->buildSql();
    
        $caipai_list = Db::name('caipai_list')
        ->alias('a')
        ->join("{$max_czid_query} b",'b.czid=a.id')
        ->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
        ->where('a.id',$kjjg_id)
        ->where('a.cp_if_open<2')
        ->order('a.cp_mb_px desc')
        ->find();
        
        $caipai_list['cp_name_new'] || $caipai_list['cp_name_new'] = $caipai_list['cp_name_old'];
        if($kjjg_id==62 || $kjjg_id==63 || $kjjg_id==72){
        	$caipai_list['qihao'] = substr($caipai_list['qihao'],4);
        }
        
        $list = Db::name('kjjg_list')
		        	->where('czid= ' . $kjjg_id . ' and kjjg>0')
		            ->order('qihao desc')
		            ->limit(20)
		            ->select();

        foreach ($list as &$kjls){
        	$kjls['kjjg'] = kjjg_split($kjls['kjjg'], $kjls['czid']);
        	$a=0;
        	/* foreach ($kjls['kjjg'] as $ss){$a+=$ss;}
        	if($kjls['czid']==36 || $kjls['czid']==44){
        		$kjls['kjjg'][]='=';
        		$kjls['kjjg'][]=$a;
        	} */
        }
        $kjjg_data_fur = Db::name('kjjg_list')->field('jztime')->where('czid',$kjjg_id)->where('jztime','>',time())->where('kjjg','0')->order('qihao','DESC')->find();
        $kjjg_data_fur['jztime'] = $kjjg_data_fur['jztime'] ? $kjjg_data_fur['jztime'] : time();
        $jztime_fur = $kjjg_data_fur['jztime'] - time();
        if($jztime_fur<=0){
        	$h_m_s['h']='开';
        	$h_m_s['m']='奖';
        	$h_m_s['s']='中';
        }else{
        	$h_m_s['h'] = floor(($jztime_fur / 3600) % 24);
        	$h_m_s['m'] = floor(($jztime_fur / 60) % 60);
        	$h_m_s['s'] = floor($jztime_fur % 60);
        	if($h_m_s['h'] <10) $h_m_s['h'] = '0'.$h_m_s['h'];
        	if($h_m_s['m'] <10) $h_m_s['m'] = '0'.$h_m_s['m'];
        	if($h_m_s['s'] <10) $h_m_s['s'] = '0'.$h_m_s['s'];
        }
        
        
        $this->assign('h_m_s',$h_m_s);
        $this->assign('jztime_fur',$jztime_fur);


		$redis = new Redis();
		$before_qihao = $redis->get('secend_'.$kjjg_id);
		if(!$before_qihao) {
			$before_qihao = Db::name('kjjg_list')->where('czid',$kjjg_id)->where('jztime','<',$kjjg_data_fur['jztime'])->order('qihao','DESC')->value('qihao');
		}
		$between =  Db::name('kjjg_list')->where(['czid'=>$kjjg_id,'qihao'=>$before_qihao])->value('kjjg');

		$this->assign('has',empty($between) ? 1 : 0);
		$this->assign('syiq',$before_qihao);

        $this->assign('caipiao',$caipai_list);
		$this->assign('kjjg_id',$kjjg_id);
		$this->assign('title','开奖公告');
        $this->assign('list', $list);
        return $this->fetch('kjjg_noe');
    }
    public function runLotteryAndroid()
    {
    	$kjjg_id = input('get.kjjg_id');
    	$max_id_query = Db::name('kjjg_list')
    	->field('MAX(id)')
    	->group('czid')
    	->buildSql();
    
    	$max_czid_query = Db::name('kjjg_list')
    	->field('czid,kjjg,qihao,jztime')
    	->where("id IN({$max_id_query})")
    	->buildSql();
    
    	$caipai_list = Db::name('caipai_list')
    	->alias('a')
    	->join("{$max_czid_query} b",'b.czid=a.id')
    	->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
    	->where('a.id',$kjjg_id)
    	->where('a.cp_if_open<2')
    	->order('a.cp_mb_px desc')
    	->find();
    
    	$caipai_list['cp_name_new'] || $caipai_list['cp_name_new'] = $caipai_list['cp_name_old'];
    
    
    	$list = Db::name('kjjg_list')
    	->where('czid= ' . $kjjg_id . ' and kjjg>0')
    	->order('qihao desc')
    	->limit(20)
    	->select();
    
    	foreach ($list as &$kjls){
    		$kjls['kjjg'] = kjjg_split($kjls['kjjg'], $kjls['czid']);
    		$a=0;
    		foreach ($kjls['kjjg'] as $ss){$a+=$ss;}
    		if($kjls['czid']==36 || $kjls['czid']==44){
    			$kjls['kjjg'][]='=';
    			$kjls['kjjg'][]=$a;
    		}
    	}
    
    	$syiqi = $caipai_list['qihao']-1;
    
    	$this->assign('syiq',$syiqi);
    	$this->assign('caipiao',$caipai_list);
    	$this->assign('kjjg_id',$kjjg_id);
    	$this->assign('title','开奖公告');
    	$this->assign('list', $list);
    	return $this->fetch('kjjg_noe2');
    }
    /**
     * 开奖走势列表
     */
    public function trend()
    {
        $this->assign('title', '走势图列表');
        $this->assign('cur', 'lottery_trend');
        $list = Db::name('caipai_list')->where('cp_if_open', '<', 2)->where('id',['<>','48'],['<>','60'],'and')
            ->order('cp_mb_px desc')
            ->select();
        
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 开奖走势详情
     * 
     * @return \think\mixed
     */
    public function trendDetails()
    {   $game_id = input('game_id');
        $this->assign('game_id',$game_id);
        $this->assign('cur', 'lottery_trend');
        $this->assign('title','走势图');
        $caizhong = Db::name('caipai_list')->where('id',$game_id)->find();
        $this->assign('caizhong',$caizhong);
        return $this->fetch('trend_details');
    }
    public function zoushi(){
    	$game_id = input('game_id');
		$this->assign('game_id',$game_id);
		$zoushi = Db::name('kjjg_list')->where('czid',$game_id)->where("kjjg>'0'")->order('qihao','DESC')->limit(24)->select();

		$trend = new Trend();
		if($game_id == 1 || $game_id == 8 || $game_id == 62){
		    $zoushi = $trend->getSscTrend($zoushi,$game_id);
		}else if($game_id == 4 || $game_id == 61 || $game_id == 63){
		    $zoushi = $trend->getPk10Trend($zoushi,$game_id);
		}else if($game_id == 36 || $game_id == 44 || $game_id == 72){
		    $zoushi = $trend->getK3Trend($zoushi,$game_id);
		}else if($game_id == 16 || $game_id == 25 || $game_id == 28){
		    $zoushi = $trend->get11x5Trend($zoushi,$game_id);
		}else if($game_id == 5 || $game_id == 7 || $game_id == 48){
		    $zoushi = $trend->getDpcTrend($zoushi,$game_id);
		}else if($game_id == 10){
		    $zoushi = $trend->getPcddTrend($zoushi,$game_id);
		}else if($game_id == 9){
		    $zoushi = $trend->getHk6Trend($zoushi,$game_id);
		}else if($game_id == 51){
		    $zoushi = $trend->getQxcTrend($zoushi,$game_id);
		}
		$caizhong = Db::name('caipai_list')->where('id',$game_id)->find();
		$this->assign('caizhong',$caizhong);
		$this->assign('zoushi',$zoushi);
    	$this->assign('title','走势图');
		
    	return $this->fetch();
    }
    
    public function zhudan(){
        $game_id = input('game_id') ? input('game_id') : 'all';
        $caizhong = Db::name('caipai_list')->where('cp_if_open',1)->field('id,cp_name_old,cp_name_new')->select();
        if(input('get.game_id')){
            $tit = Db::name('caipai_list')->where('id',$game_id)->field('id,cp_name_old,cp_name_new')->find();
            $this->assign('tit',$tit);
        }
        $this->assign('game',$game_id);
        $this->assign('caizhong',$caizhong);
        $this->assign('cur','lottery_trend');
        return $this->fetch();
    }
    
    public function zhudanData(){
    	$game_id = input('game_id') ? input('game_id') : 'all';
    	$type = input('get.type') ? input('get.type') : 'all';
    	$page = input('get.page') ? input('get.page') : 0;
    	$riqi = input('get.day') ? input('day') : '今天';
    	$name=$this->user_all['username'];

    	$day = 'today';
    	if($riqi=='今天'){$day = 'today';}
    	if($riqi=='本周'){$day = 'week';}
    	if($riqi=='昨天'){$day = 'yesterday';}
    	if($riqi=='本月'){$day = 'month';}
    	if($riqi=='上月'){$day = 'last month';}
    	//$time = timeSelection($day);
    	if($game_id == 'all'){
    		$where = array('a.username'=>$name);
    		$w_yzj = array('a.username'=>$name,'a.zjje'=>['>',0.001]);
    	}else{
    		$where = array('a.username'=>$name,'a.czid'=>$game_id,);
    		$w_yzj = array('a.username'=>$name,'a.czid'=>$game_id,'a.zjje'=>['>',0.001]);
    	}

    	
    	$data_array = array();
    	$yzj=array();
    	$cd=array();
    	$qb_caizhong=array();
    	$ok_qb = array();
    	$swyzj = array();
    	if($name) {
    	    $user = Db::name('username')->where('username',$name)->value('swzh');
    	    $nbzh = Db::name('username')->where('username',$name)->value('nbzh');
    	    $page_count = $type == 'all' ? 10 : 20;
        	    
        	    if($type == 'all' || $type == 2){
        	        $qb_caizhong = Db::name('touzhu_list_no')
        	        ->alias('a')
        	        ->join('__CAIPAI_LIST__ b','a.czid=b.id')
        	        ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
        	        ->where($where)->whereTime('a.tz_time',$day)->order('a.qihao','DESC')->page($page,25)->select();
        	    }
        	    if($type == 'all'){
        	        if($user == 1 || $nbzh==1){
        	            $ok_qb = Db::name('touzhu_list_sw')->alias('a')->join('__CAIPAI_LIST__ b','b.id=a.czid')
        	            ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
        	            ->where($where)->whereTime('a.tz_time',$day)->order('a.qihao','DESC')->page($page,$page_count)->select();
        	        }else{
        	            $ok_qb = Db::name('touzhu_list_ok')
			        	            ->alias('a')
			        	            ->join('__CAIPAI_LIST__ b','a.czid=b.id')
			        	            ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
			        	            ->where($where)->whereTime('a.tz_time',$day)->order('a.qihao','DESC')->page($page,$page_count)->select();
        	        }
        	         
        	    }
        	    if($type == 1){
        	        if($user == 1 || $nbzh==1){
        	            $yzj = Db::name('touzhu_list_sw')->alias('a')->join('__CAIPAI_LIST__ b','b.id=a.czid')
                	            ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                	            ->where($w_yzj)->whereTime('a.tz_time',$day)->order('a.qihao','DESC')->page($page,$page_count)->select();
        	        }else{
        	            $yzj = Db::name('touzhu_list_ok')->alias('a')
        	                    ->join('__CAIPAI_LIST__ b','b.id=a.czid')
                	            ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                	            ->where($w_yzj)->whereTime('a.tz_time',$day)->order('a.qihao','DESC')->page($page,$page_count)->select();
        	        }
        	    }
        	    
        	    if($type == 'all' || $type == 3){
                	$cd = Db::name('touzhu_list_zh')
            			    	->alias('a')
            			    	->join('__CAIPAI_LIST__ b','b.id=a.czid')
            			    	->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
            			    	->where($where)->whereTime('a.tz_time',$day)->order('a.qihao','DESC')->page($page,$page_count)->select();
        	    }
        	    $data_array = array_merge($qb_caizhong,$ok_qb,$yzj,$cd);
                if(isset($data_array[0])){
                    $data_array = arraySequence($data_array,'tz_time');
                }
    	}
    	return json($data_array);
    }

    /* 彩种介绍 */
    public function guize(){
    	$gameid = input('game_id') ? input('game_id') : '';
    	$gid = input('gid') ? input('gid') : 1;
    	if($gameid) {
    	    $info = Db::name('lottery_classify')->field('id,class_name_lottery')->where("czids REGEXP '(^|,){$gameid}($|,)'")->find();
    	    $class_name_lottery = $info['class_name_lottery'];
    	    $gid = $info['id'];
    	} else {
    	    $info = Db::name('lottery_classify')->field('id,class_name_lottery')->where('id',$gid)->find();
    	    $class_name_lottery = $info['class_name_lottery'];
    	    $gid = $info['id'];
    	}
    	$this->assign('gid',$gid);
    	$this->assign('class_name_lottery',$class_name_lottery);
    	return $this->fetch();
    }
    public function zaixiankefu(){
    	
    	return $this->fetch();
    }
    
    /* 玩法规则 */
	public function gz_list(){
		$gameid = input('game_id') ? input('game_id') : '';
    	$gid = input('gid') ? input('gid') : '1';
    	if($gameid) {
    	    $cp = Db::name('lottery_classify')->where("czids REGEXP '(^|,){$gameid}($|,)'")->find();
    	    $wanfalist = Db::name('play_func')->where('czid',$gameid)->select();
    	    $this->assign('game_id',$gameid);
    	} else {
    	    $cp = Db::name('lottery_classify')->where('id',$gid)->find();
    	    $czid_arr = explode(',',$cp['czids']);
    	    $wanfalist = Db::name('play_func')->where('czid',$czid_arr[0])->select();
    	    $this->assign('game_id',$czid_arr[0]);
    	}

    	$this->assign('cp',$cp);
    	$this->assign('wflist',$wanfalist);
		return $this->fetch();
	}
	public function active(){
		$duquhjud = Db::name('xt_zzhd')->where('ifopen','<',2)->select();
		$i = 0;
		$result='';
		 foreach ($duquhjud as &$item){
            if ($item['tjzq'] == '1') {
                $year_now = date('Y-m-d', strtotime('-1 day')) . " 00:00:00";
                $year_now_1 = date('Y-m-d', strtotime('-1 day')) . " 23:59:59";
                $hdtime_1 = strtotime($year_now_1);
                $hdtime = strtotime($year_now);
                $taday = date('Y-m-d') . "00:00:00";
                $taday = strtotime($taday);
            }
            if ($item['tjzq'] == '2') {
                $year_now = date('Y-m-d', strtotime('-1 week last monday')) . "-01 00:00:00";
                $year_now_1 = date('Y-m-d', strtotime('last sunday')) . " 23:59:59";
                $hdtime_1 = strtotime($year_now_1);
                $hdtime = strtotime($year_now);
                $taday = date('Y-m-d', strtotime('sunday -6 day')) . " 00:00:00";
                $taday = strtotime($taday);
            }
            if ($item['tjzq'] == '3') {
                $year_now = date('Y-m', strtotime('-1 month')) . "-01 00:00:00";
                $year_now_1 = date('Y-m') . "-01 00:00:00";
                $hdtime_1 = strtotime($year_now_1);
                $hdtime = strtotime($year_now);
                $taday = date('Y-m') . "-01 00:00:00";
                $taday = strtotime($taday);
            }

			$item['tj_czje'] = explode('-', $item['tj_czje']);
			$item['tj_tzje'] = explode('-', $item['tj_tzje']);
			$item['hd_je'] = explode('-', $item['hd_je']);
			 $item['tj_czje']=array_filter($item['tj_czje']);
			$item['tj_tzje']=array_filter($item['tj_tzje']);
			$item['hd_je']=array_filter($item['hd_je']); 
			$i = count($item['hd_je']);
            $st=0;
			for($q=0;$q<$i;$q++){
				$item['sur'][]=[$item['tj_czje'][$q],$item['tj_tzje'][$q],$item['hd_je'][$q]];

			}
		

            $czjes = Db::name('chongzhi_list')->where("username='{$this->user_all["username"]}' AND cztime>'{$hdtime}' AND cztime<'{$hdtime_1}' AND zt=1 ")->sum("je");
            $tzje=Db::name('touzhu_list_ok')->where("username='{$this->user_all["username"]}' AND tz_time>'{$hdtime}' AND tz_time<'{$hdtime_1}' ")->sum("tzje");
            $iflingq = Db::name('caijin_list')->where("username='{$this->user_all["username"]}' AND zzhdid='{$item["id"]}' AND fstime>='{$taday}' ")->find();
            if ($czjes < min($item['tj_czje']) || $tzje < min($item['tj_tzje']) || $iflingq['id'] > 0){
                $result=0;
            }else{
                 $result=1;
            }
            
            if($result==1){
                $item['sty']="#f0f";
                $item['islingqu']="可以领取";
            }else {
               $item['sty']="#999";
               $item['islingqu']="未达标，无法领取";
            }

            /*var_dump($item['tj_tzje']);*/
		} 
		unset($item);
		$this->assign('result',$result);
		$hdid = input('hdid')?input('hdid'):$duquhjud[0]['id'];
		$huodong = Db::name('xt_zzhd')->where('id',$hdid)->find();
		$this->assign('i',$i);
		$this->assign('rr',$huodong);
		$this->assign('query',$duquhjud);
		return $this->fetch();
	}
}
